import Vue from 'vue';
import {
    api,
} from '@/lib/api';
import {
    url,
} from '@/lib/utils';

import { Module } from 'vuex';
import { RootState } from '@/types/store';

export interface AddressState {
    /** 列表，保存id */
    list: number[] | null;
    /** 地址详情 */
    detail: {
        [addressId: number]: Address.ListItem;
    };
    /** 默认地址 */
    default: Address.ListItem | null;
}

const m: Module<AddressState, RootState> = {
    namespaced: true,
    state: {
        list: null,
        detail: {},
        default: null,
    },
    mutations: {
        /**
         * 记录地址列表
         */
        setList(state, list) {
            const ids = [];
            for (const address of list) {
                ids.push(address.id);
                Vue.set(state.detail, address.id, address);
            }
            state.list = ids;
        },
        /**
         * 删除addressId地址
         */
        remove(state, addressId) {
            if (!state.list) {
                return;
            }
            const pos = state.list.indexOf(Number(addressId));
            if (pos >= 0) {
                state.list.splice(pos, 1);
            }
        },
        /**
         * 记录默认地址
         */
        setDefault(state, detail) {
            state.default = detail;
        },
        /**
         * 记录地址详情
         */
        setDetail(state, { addressId, detail }) {
            Vue.set(state.detail, addressId, detail);
        },
    },

    actions: {
        /**
         * 获取列表
         */
        async getList(context) {
            const res = await api.get({
                url: `${url}/api/user/address_list`,
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                context.commit('setList', res.data.data);
            }
            return res;
        },
        /**
         * 获取默认地址
         */
        async getDefault(context) {
            const res = await api.get({
                url: `${url}/api/user/default_address`,
            });
            if (res.statusCode === 200 && res.data.status === 0 && res.data.data) {
                const detail = res.data.data;
                context.commit('setDefault', detail);
                context.commit('setDetail', {
                    addressId: detail.id,
                    detail,
                });
            }
            return res;
        },
        /**
         * 创建
         */
        async create(context, { title, name, mobile, province, city, district, detail, isDefault }) {
            const res = await api.post({
                url: `${url}/api/user/address`,
                data: {
                    title,
                    name,
                    mobile,
                    province,
                    city,
                    district,
                    detail,
                    is_default: isDefault,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                await context.dispatch('getList');
            }
            return res;
        },
        /**
         * 更新
         */
        async update(context, { addressId, title, name, mobile, province, city, district, detail, isDefault }) {
            const data = {
                id: addressId,
                title,
                name,
                mobile,
                province,
                city,
                district,
                detail,
                is_default: isDefault,
            };
            const res = await api.post({
                url: `${url}/api/user/update_address`,
                data,
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                context.commit('setDetail', {
                    addressId,
                    detail: data,
                });
            }
            return res;
        },
        /**
         * 删除
         */
        async delete(context, { addressId }) {
            const res = await api.post({
                url: `${url}/api/user/address/delete`,
                data: {
                    id: addressId,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                context.commit('remove', addressId);
            }
            return res;
        },
    },
};

export default m;
